//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus.AsyncTasks;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;

import com.bc004346.travelus.DataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.Model.Domain.Flight;


/**
 * Created by rstozkov on 1/29/14.
 * Handles async checking in event
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class CheckinTaskFragment extends Fragment {

    public static interface TaskCallbacks {
        void onCheckinPostExecute(Flight flight, Exception e);
    }

    private TravelUsScreenScraper mTravelUs;
    private TaskCallbacks mCallbacks;
    private Flight mFlight;

    public CheckinTaskFragment(TravelUsScreenScraper screenScraper, Flight flight) {
        mTravelUs = screenScraper;
        mFlight = flight;
    }


    //<editor-fold desc="Overrides">

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (TaskCallbacks) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        new CheckinTask().execute(mFlight);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    //</editor-fold>

    private class CheckinTask extends AsyncTask<Flight, Void, Flight> {

        private Exception ex = null;

        @Override
        protected void onPostExecute(Flight flight) {

            if (mCallbacks != null) {
                mCallbacks.onCheckinPostExecute(flight, ex);
            }
        }

        @Override
        protected Flight doInBackground(Flight... params) {

            Flight flight = null;
            try {
                flight = params[0];
                flight.setIsCheckedIn(mTravelUs.doCheckin(String.valueOf(mTravelUs.getBadge()), flight.getPnr(), flight.getOrigin()));
            } catch (Exception e) {
                ex = e;
            }
            return flight;
        }
    }
}
